/**
 * 
 */
package cn.edu.bjtu.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.Properties;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.edu.bjtu.pojo.ResCode;


/**
 * @ClassName: BaseController
 * @Description: TODO
 * @author: Richard
 * @ChineseName Li Yao
 * @date 2017/8/25 PM 1:07:04
 * 
 */
public abstract class BaseController
        implements ServletContextAware, ApplicationEventPublisherAware {
	// 
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    protected static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    protected static ObjectMapper mapper = new ObjectMapper();
    protected ApplicationEventPublisher applicationEventPublisher;

    static {
        mapper.setDateFormat(sdf);
    }

    public static final ResCode SUCCESS = new ResCode(0, "success");
    public static final ResCode ERROR = new ResCode(400, "error");
    public static final ResCode ParamsError = new ResCode(400, "params error");

    protected ModelAndView renderView(String viewName, Map<String, ?> model) {
        return new ModelAndView(viewName, model);
    }

    protected ModelAndView renderView(String viewName, String name, Object obj) {
        return new ModelAndView(viewName, name, obj);
    }

    protected String jsonDump(Object object) {
        try {
            return mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return jsonDump(ERROR);
        }
    }

    protected <T> T fromJson(String content, Class<T> clz) {
        try {
            return mapper.readValue(content, clz);
        } catch (JsonParseException e) {
            e.printStackTrace();
            return null;
        } catch (JsonMappingException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    protected static WebApplicationContext wac = null;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;

    }

    protected String webAppPath() {
        return wac.getServletContext().getRealPath("/");
    }
    @Override
    public void setServletContext(ServletContext servletContext) {
        wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);

    }


}
